package net.xdclass.onlinexdclass.templates;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class JdbcTemplateUtil {


    @Autowired
    private JdbcTemplate jdbcTemplate;

    //增加修改删除都用这个
    public void insertAndUpdate(String sql, Object[] obj) {
        jdbcTemplate.update(sql, obj);
    }

    //查询
    //查询返回对象集合
    public <T> List selectListPojo(String sql, Class<T> t, Object[] obj) {
        List<Object> list = jdbcTemplate.query(sql, obj, (RowMapper<Object>) new BeanPropertyRowMapper<T>(t));
        return (List) list;

    }

    //查询返回对象
    public <T> T selectPojo(String sql, Class<T> t, Object[] obj) {
        Object object = jdbcTemplate.queryForObject(sql, obj, new BeanPropertyRowMapper<T>(t));
        return (T) object;
    }

    //查询返回List<Map<String, Object>>
    public <T> List<Map<String, Object>> selectListMap(String sql, Object[] obj) {
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql, obj);
        return (List) list;
    }
}